#include<iostream>
#include<cstdio>
#include<cmath>
#define MAX 0x3f3f3f3f
using namespace std;
int n,a[11],b[11];
int sum=MAX;
void dfs(int i,int sum1,int sum2)
{

	if(i>n-1)
	{
		if(sum1==1&&sum2==0)
		return;
		sum=min(sum,abs(sum1-sum2));
		return;
	}
	dfs(i+1,sum1*a[i],sum2+b[i]);
	dfs(i+1,sum1,sum2);
}
int main()
{
	
	cin>>n;
	for(int i=0;i<n;i++)
	{
		cin>>a[i]>>b[i];
	}

	dfs(0,1,0);
	cout<<sum;
	return 0;
}